#! /bin/bash

INSTALL_PATH=~/soft
node=slave
JAVA_HOME=""
mkdir -p ${INSTALL_PATH}
cd ${INSTALL_PATH}

if [ ! -d "elasticsearch_slave" ]; then
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.0-linux-x86_64.tar.gz
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.0-linux-x86_64.tar.gz.sha512
    shasum -a 512 -c elasticsearch-7.12.0-linux-x86_64.tar.gz.sha512
    tar -xzf elasticsearch-7.12.0-linux-x86_64.tar.gz
    mv elasticsearch-7.12.0 elasticsearch_slave
    rm elasticsearch-7.12.0-linux-x86_64.tar.gz*
    rm elasticsearch-7.12.0-linux-x86_64.tar.gz.sha512*
fi

cd elasticsearch_slave
cp config/jvm.options config/jvm.options.bak
cp config/elasticsearch.yml config/elasticsearch.yml.bak

echo "
-Xms1000m
-Xmx1000m
" >>config/jvm.options

echo "jvm.options 配置完成"

echo "
#每个节点的名字唯一
node.name: slave
#数据目录
path.data: /root/data/slave
bootstrap.memory_lock: true
#监听ip和localhost
network.host: 0.0.0.0
#http网络端口
http.port: 9200
# 集群发现节点，所有节点的ip
discovery.seed_hosts: [\"172.17.0.4\", \"172.17.0.5\"]
#初始启动master
cluster.initial_master_nodes: [\"master\"]
#是否主节点
node.master: false
#是否存放数据
node.data: true

#启用xpack
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.client_authentication: required
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
">config/elasticsearch.yml
echo "elasticsearch.yml配置完成"

cp ${INSTALL_PATH}/elasticsearch_master/config/elastic-certificates.p12 ${INSTALL_PATH}/elasticsearch_slave/config/
cp ${INSTALL_PATH}/elasticsearch_master/config/elastic-stack-ca.p12 ${INSTALL_PATH}/elasticsearch_slave/config/
